﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using AbbrWordWikiDB;
using System.Windows.Navigation;
using System.ComponentModel;

namespace AbbrWordWiki
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            App.ViewModel.PropertyChanged += new PropertyChangedEventHandler(MainViewModelInMainPage_PropertyChanged);
        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.NewButton.Opacity = 0.5;
            if (this.SearchTextBox.Text.Equals(string.Empty))
                LoadShowCaseData();
            else
                LoadMainListBox();

            if (App.ViewModel.IsDataLoaded)
            {
                this.ProgressBar.Visibility = Visibility.Collapsed;
                this.StatusTextBlock.Visibility = Visibility.Collapsed;
            }
        }

        // When page is navigated to, set data context 
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            // Set the data context of the listbox control to the sample data
            if (DataContext == null)
                DataContext = App.ViewModel;
        }

        private void LoadShowCaseData()
        {
            this.MainListBox.Items.Clear();

            t_AbbrWordWiki showCaseWord = new t_AbbrWordWiki();
            showCaseWord.Word = "ASU";
            showCaseWord.FullWord = "Air Separation Unit";
            showCaseWord.Description = "";

            this.MainListBox.Items.Add(showCaseWord);

            showCaseWord = new t_AbbrWordWiki();
            showCaseWord.Word = "APT";
            showCaseWord.FullWord = "Accident Predictive Technique";
            showCaseWord.Description = "";

            this.MainListBox.Items.Add(showCaseWord);

            showCaseWord = new t_AbbrWordWiki();
            showCaseWord.Word = "EAR";
            showCaseWord.FullWord = "Expenditure Authorization Request";
            showCaseWord.Description = "";

            this.MainListBox.Items.Add(showCaseWord);


            showCaseWord = new t_AbbrWordWiki();
            showCaseWord.Word = "EPR";
            showCaseWord.FullWord = "Enterprise Platform Renewal";
            showCaseWord.Description = "";

            this.MainListBox.Items.Add(showCaseWord);

            showCaseWord = new t_AbbrWordWiki();
            showCaseWord.Word = "ZFA";
            showCaseWord.FullWord = "Zero Footprint Application";
            showCaseWord.Description = "";

            this.MainListBox.Items.Add(showCaseWord);
        }

        private void SearchTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            int cursorLocation = this.SearchTextBox.SelectionStart;

            this.SearchTextBox.Text = this.SearchTextBox.Text.ToUpper();

            SearchTextBox.SelectionStart = cursorLocation;

            LoadMainListBox();
        }

        private void LoadMainListBox()
        {
            this.MainListBox.Items.Clear();

            foreach (t_AbbrWordWiki abbrWord in App.ViewModel.AbbrWordsViewData)
            {
                if (abbrWord.Word.ToLower().StartsWith(this.SearchTextBox.Text.ToLower()))
                {
                    this.MainListBox.Items.Add(abbrWord);
                }
            }

            if (this.MainListBox.Items.Count > 0) this.MainListBox.SelectedIndex = 0;

            if (this.MainListBox.Items.Count == 0)
                this.NewButton.Opacity = 1;
            else
                this.NewButton.Opacity = 0.5;
        }

        public void MainViewModelInMainPage_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            this.StatusTextBlock.Text = "Loaded";
            this.ProgressBar.Visibility = Visibility.Collapsed;
            this.StatusTextBlock.Visibility = Visibility.Collapsed;

            this.StartAnimationFromResource("LoadedTextAway", this.StatusTextBlock as FrameworkElement);
            this.StartAnimationFromResource("LoadedBarAway", this.ProgressBar as FrameworkElement);
        }

        private void ClientItem_Loaded(object sender, RoutedEventArgs e)
        {
            this.StartAnimationFromResource("ClientItem_Loaded", sender as FrameworkElement);
            ((StackPanel)sender).Loaded -= this.ClientItem_Loaded;
        }

        private void StartAnimationFromResource(string animationName, FrameworkElement element)
        {
            Storyboard sb = element.Resources[animationName] as Storyboard;
            Storyboard.SetTarget(sb, element);
            sb.Begin();
        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            // If selected index is -1 (no selection) do nothing
            if (MainListBox.SelectedIndex == -1)
                return;

            string urlDetail = "/DetailsPage.xaml?selectedWord=" + ((Image)sender).Tag;
            // Navigate to the new page
            NavigationService.Navigate(new Uri(urlDetail, UriKind.Relative));
        }

        private FrameworkElement selectedItem;

        private void StackPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.selectedItem == sender)
                return;

            Storyboard sb;
            TextBlock target;

            if (this.selectedItem != null)
            {
                target = ((System.Windows.Controls.Panel)(((System.Windows.Controls.Panel)(this.selectedItem)).Children[1])).Children[0] as TextBlock;
                sb = selectedItem.Resources["ClientsListAnimation_HighLight"] as Storyboard;

                if (sb.GetCurrentState() != ClockState.Stopped)
                {
                    sb.Stop();
                }

                sb = selectedItem.Resources["ClientsListAnimation_UnHighLight"] as Storyboard;
                Storyboard.SetTarget(sb, target);
                sb.Begin();
            }

            selectedItem = sender as FrameworkElement;
            sb = selectedItem.Resources["ClientsListAnimation_UnHighLight"] as Storyboard;

            if (sb.GetCurrentState() != ClockState.Stopped)
            {
                sb.Stop();
            }

            sb = selectedItem.Resources["ClientsListAnimation_HighLight"] as Storyboard;
            target = ((System.Windows.Controls.Panel)(((System.Windows.Controls.Panel)(((FrameworkElement)sender))).Children[1])).Children[0] as TextBlock;
            Storyboard.SetTarget(sb, target);
            sb.Begin();
        }

        private void NewButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            // If selected index is -1 (no selection) do nothing
            if (this.SearchTextBox.Text.Equals(string.Empty))
                return;

            // Navigate to the new page
            NavigationService.Navigate(new Uri("/EditWordPage.xaml?newWord=" + this.SearchTextBox.Text, UriKind.Relative));
        
        }
    }
}